﻿
Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Win
Imports Infragistics
Public Class IUltraCombo
    Inherits UltraCombo
    Dim flagCbo As Boolean = False
    Dim dt As DataTable
    ''' <summary>
    ''' set giao diện (tự động co dãn theo độ dài cột dữ liệu)
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub setGiaoDien()
        Me.DisplayLayout.Override.RowAlternateAppearance.BackColor = Color.LightCyan
        'thu gon combobox
        Me.DisplayLayout.Override.RowSelectorNumberStyle = RowSelectorNumberStyle.VisibleIndex
        Me.DisplayLayout.Override.RowAlternateAppearance.BackColor = Color.LightCyan
        Me.DisplayLayout.Override.RowSelectorAppearance.ForeColor = Color.Black
        Me.DisplayLayout.Override.RowSelectorWidth = 30
        Me.DisplayLayout.Override.SelectedRowAppearance.BackColor = Color.Pink
        'co dãn theo dữ liệu
        Me.DisplayLayout.Bands(0).PerformAutoResizeColumns(False, PerformAutoSizeType.AllRowsInBand, True)
        'mở rộng các cột cho vừa - mặc định
        'me.DisplayLayout.AutoFitStyle = AutoFitStyle.ResizeAllColumns
        'cho sắp xếp các cột khi click lên header mỗi cột
        Me.DisplayLayout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortMulti

        'me.DisplayLayout.Override.RowSelectorAppearance.BackColor = Color.DodgerBlue
        'me.DisplayLayout.Override.RowSelectorAppearance.BackColor2 = Color.White
        Me.DisplayLayout.Override.RowSpacingAfter = 0
        Me.DisplayLayout.Override.RowSpacingBefore = 0
        Me.DisplayLayout.Override.RowSizingAutoMaxLines = 1
        Me.DisplayLayout.Override.HeaderAppearance.TextHAlign = HAlign.Center
        Me.DisplayLayout.Override.RowAppearance.BorderColor = Color.Gray
        Me.UseFlatMode = DefaultableBoolean.True
        Me.DisplayLayout.Override.HeaderAppearance.FontData.Bold = DefaultableBoolean.True
        Me.DisplayLayout.Override.HeaderAppearance.TextHAlign = HAlign.Center
        setFormatUltraGrid(Me)
        Me.AutoCompleteMode = Win.AutoCompleteMode.None
    End Sub
    ''' <summary>
    ''' set giao diện (co dãn theo độ dài dữ liệu) và ẩn heaader cột 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub setGiaoDien_An_ColumnHeader()
        setGiaoDien()
        Me.DisplayLayout.Bands(0).ColHeadersVisible = False
    End Sub
    Private Sub setFormatUltraGrid(ByVal cbo As UltraCombo)
        Dim dataType As String
        For index As Integer = 0 To cbo.Rows.Band.Columns.Count - 1
            dataType = cbo.Rows.Band.Columns(index).DataType.ToString
            If (dataType = "System.DateTime" Or dataType = "System.Double" Or dataType = "System.Decimal" Or dataType = "System.Int16" Or dataType = "System.Int32" Or dataType = "System.Int64") Then
                cbo.Rows.Band.Columns(index).CellAppearance.TextHAlign = HAlign.Right
            End If
        Next
    End Sub


    Private Sub IUltraCombo_Click(sender As Object, e As System.EventArgs) Handles Me.Click
        setCboClick(sender, e, Me)
    End Sub

    Private Sub IUltraCombo_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Me.flagCbo = Me.getKeyDown(e, CType(sender, UltraCombo))
    End Sub

    Private Sub IUltraCombo_TextChanged(sender As Object, e As System.EventArgs) Handles Me.TextChanged
        If flagCbo Then
            M_Ultragrid.setComboTimKiemNhanh(dt, Me)
            'focus ở vị trí cuối
            Me.Textbox.SelectionStart = Me.Text.Length
        End If
    End Sub
    Private Sub setCboClick(ByVal sender As Object, ByVal e As System.EventArgs, ByVal cbo As UltraCombo)
        With cbo
            .Textbox.SelectionStart = 0
            .Textbox.SelectionLength = Len(.Text)
            .Focus()
            If cbo.IsDroppedDown = False Then
                Me.ToggleDropdown()
            End If
        End With
    End Sub
    Private Function getKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs, ByVal cboToggleDropDrown As UltraCombo) As Boolean
        If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Then
            If cboToggleDropDrown.IsDroppedDown = False Then
                cboToggleDropDrown.ToggleDropdown()
            End If
            Return False
        End If
        If e.KeyCode <> Keys.Enter Then
            If cboToggleDropDrown.IsDroppedDown = False Then
                cboToggleDropDrown.ToggleDropdown()
            End If
            Return True
        End If
        If e.KeyCode = Keys.Enter Then
            If M_Ultragrid.getKiemTraItemTrongDatatable(dt, cboToggleDropDrown.Text, cboToggleDropDrown.ValueMember) = False Then
                cboToggleDropDrown.PerformAction(UltraComboAction.FirstRow)
                Return False
            End If
        End If
    End Function
    ''' <summary>
    ''' set datatable tương ứng với iultracombo (để hỗ trợ tìm kiếm)
    ''' </summary>
    ''' <param name="dt"></param>
    ''' <remarks></remarks>
    Public Sub setDataTable(ByVal dt As DataTable)
        Me.dt = dt
    End Sub
End Class
